package com.java.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.java.entity.BsStudent;
import com.java.entity.dtoVo.BsStudentDto;
import com.java.service.BsStudentService;

/**
 * 自定义监听器，对下载的excel中的数据进行校验
 */
@Component
public class StudentListener extends AnalysisEventListener<BsStudentDto> {

	private static BsStudentService studentService;
	 
    @Autowired
    public void setBsStudentService(BsStudentService studentService){
        this.studentService = studentService;
    }
    List<String> numbers = new ArrayList<>();

    /**
     * 每解析一行，回调该方法
     *
     * @param data
     * @param context
     */
    @Override
    public void invoke(BsStudentDto student, AnalysisContext context) {
    	String number = student.getStudentNumber();
        if (MyTools.isEmpty(number)) {
            throw new RuntimeException(String.format("第%s行学号为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentName())) {
            throw new RuntimeException(String.format("第%s行名称为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentCollege())) {
            throw new RuntimeException(String.format("第%s行学院为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentClass())) {
            throw new RuntimeException(String.format("第%s行班级为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentMajor())) {
            throw new RuntimeException(String.format("第%s行专业为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentSex())) {
            throw new RuntimeException(String.format("第%s行性别为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentMobile())) {
            throw new RuntimeException(String.format("第%s行手机号为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentEmail())) {
            throw new RuntimeException(String.format("第%s行邮箱号为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (MyTools.isEmpty(student.getStudentIntakeTime())) {
            throw new RuntimeException(String.format("第%s行入学时间为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (numbers.contains(number)) {
            throw new RuntimeException(String.format("第%s行学号已重复，请核实", context.readRowHolder().getRowIndex() + 1));
        } else {
        	BsStudent info = studentService.getStudentNum(number);
        	if(null != info) {
        		 throw new RuntimeException(String.format("第%s行学号已存在，请不要重复", context.readRowHolder().getRowIndex() + 1));
        	}
        	numbers.add(number);
        }
    }

    /**
     * 出现异常回调
     *
     * @param exception
     * @param context
     * @throws Exception
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        if (exception instanceof ExcelDataConvertException) {
            /**从0开始计算*/
            int columnIndex = ((ExcelDataConvertException) exception).getColumnIndex() + 1;
            int rowIndex = ((ExcelDataConvertException) exception).getRowIndex() + 1;
            String message = "第" + rowIndex + "行，第" + columnIndex + "列" + "数据格式有误，请核实";
            throw new RuntimeException(message);
        } else if (exception instanceof RuntimeException) {
            throw exception;
        } else {
            super.onException(exception, context);
        }
    }

    /**
     * 解析完,全部回调
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    	numbers.clear();
    }
}

